package com.wangyu.talents.core.service.impl;

import com.google.common.collect.Lists;
import com.wangyu.talents.core.entity.BaseEntity;
import com.wangyu.talents.core.entity.Page;
import com.wangyu.talents.core.persistence.BaseMapper;
import com.wangyu.talents.core.service.BaseService;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 通用服务层实现
 *
 * @author wangyu
 * @Date 2018-09-02 12:06
 */
@Service
@Transactional
public class BaseServiceImpl<T extends BaseEntity> implements BaseService<T> {

  @Autowired
  protected BaseMapper<T> baseMapper;

  @Override
  public T get(Serializable id) {
    return baseMapper.get(id);
  }

  @Override
  public T get(T entity) {
    return baseMapper.get(entity);
  }

  @Override
  public T findOne(Map<String, Object> map) {
    return baseMapper.findOne(map);
  }

  @Override
  public List<T> findList(Map<String, Object> map) {
    return baseMapper.findList(map);
  }

  @Override
  public List<T> findAll() {
    return baseMapper.findList(new HashMap<String, Object>());
  }

  @Override
  public List<T> findByIds(Long[] ids) {
    if (ids != null && ids.length > 0) {
      return baseMapper.findByIds(ids);
    }
    return Lists.newArrayList();
  }

  @Override
  public Page<T> findList(Page<T> page) {
    if (page == null) {
      return null;
    }

    List<T> records = baseMapper.findList(page.getParam());
    int countNums = records.size();
    page = new Page<>(page.getCurrentPage(), page.getPageSize(), countNums);
    page.setItems(records);
    return page;
  }

  @Override
  public long countNums(Map<String, Object> map) {
    return baseMapper.countNums(map);
  }

  @Override
  public int insert(T entity) {
    return baseMapper.insert(entity);
  }

  @Override
  public int update(T entity) {
    return baseMapper.update(entity);
  }

  @Override
  public int delete(T entity) {
    return baseMapper.delete(entity);
  }

  @Override
  public int delete(Serializable id) {
    return baseMapper.delete(id);
  }

  @Override
  public int batchDelete(Long[] ids) {
    return baseMapper.batchDelete(ids);
  }
}
